#include "jost/Parser.hpp"

jost::Parser::Parser() 
{}

jost::Parser::~Parser() 
{}



jost::Parser&
jost::Parser::instance() throw()
{
    static Parser instance;
    return instance;
}

jost::Object*
jost::Parser::parse(const char* stream) throw (jost::MalformedJSONStream)
{
    jost::Object* o;
    parse_info<> info = boost::spirit::classic::parse(stream, this->json_ >> !end_p, space_p);
    if (info.full)
        o = this->json_.parsed_object();
    else {
        std::string error_msg = "Invalid JSON Object (";
        error_msg += info.stop;
        this->json_.clean_up();
        throw jost::MalformedJSONStream(error_msg);
    }
    
    return o;
}


jost::Object*
jost::Parser::parse(const std::string& stream) throw (jost::MalformedJSONStream)
{
    return this->parse(stream.c_str());
}

jost::Object*
jost::Parser::parse(std::istream& stream) throw (jost::MalformedJSONStream)
{
    char buf[JOST_FILE_CHUNK];
    std::string str;
    
    while (!stream.eof()) {
        stream.read(buf, JOST_FILE_CHUNK);
        str += buf;
    }
    
    return this->parse(str);
}

